package tk.hongbo.biometric

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt
import androidx.biometric.BiometricPrompt.AuthenticationResult
import com.google.android.material.snackbar.Snackbar
import tk.hongbo.biometric.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.textView.text = JNIUtils().getPwd()

        binding.button.setOnClickListener {
            if (checkFinger()) {
                BiometricHelper.authenticate(this, object :
                    BiometricPrompt.AuthenticationCallback() {
                    override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
                        Snackbar.make(binding.root, errString, Snackbar.LENGTH_SHORT).show()
                    }

                    override fun onAuthenticationSucceeded(result: AuthenticationResult) {
                        Snackbar.make(binding.root, "验证成功", Snackbar.LENGTH_SHORT).show()
                    }

                    override fun onAuthenticationFailed() {
                        Snackbar.make(binding.root, "验证失败", Snackbar.LENGTH_SHORT).show()
                    }
                })
            }
        }
    }

    private fun checkFinger(): Boolean {
        return when (BiometricHelper.isFingerprintAvailable(this)) {
            BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE -> {
                Snackbar.make(binding.root, "传感器当前不可用，清稍后再试", Snackbar.LENGTH_SHORT).show()
                false
            }
            BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED -> {
                Snackbar.make(binding.root, "信息没有录入，比如还没录入指纹", Snackbar.LENGTH_SHORT).show()
                false
            }
            BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE -> {
                Snackbar.make(binding.root, "没有合适的传感器或者没设置密码，例如手机没有指纹传感器", Snackbar.LENGTH_SHORT)
                    .show()
                false
            }
            BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED -> {
                Snackbar.make(binding.root, "传感器存在已知的漏洞，在更新修复漏洞前，传感器不可用", Snackbar.LENGTH_SHORT)
                    .show()
                false
            }
            BiometricManager.BIOMETRIC_ERROR_UNSUPPORTED -> {
                Snackbar.make(binding.root, "设置的一些验证条件，当前手机的Android版本无法满足", Snackbar.LENGTH_SHORT)
                    .show()
                false
            }
            BiometricManager.BIOMETRIC_STATUS_UNKNOWN -> {
                Snackbar.make(
                    binding.root,
                    "不知道是否可以进行验证。通常在旧版本的Android手机上出现，当出现这个错误是，仍然可以尝试进行验证足",
                    Snackbar.LENGTH_SHORT
                ).show()
                false
            }
            BiometricManager.BIOMETRIC_SUCCESS ->
                true
            else -> {
                Snackbar.make(binding.root, "不支持生物识别", Snackbar.LENGTH_SHORT).show()
                false
            }
        }
    }
}